<?php
/* 
 * withdrawservice definition
 * @author li.junfu
 */

require_once BaseUtil::getModulePath("base")  .  '/services/BaseService.php';
require_once BaseUtil::getModulePath("trade")  .  '/models/AccountAgentTradeLogDao.php';
require_once BaseUtil::getModulePath("trade")  .  '/models/AccountAgentTotalDao.php';
require_once BaseUtil::getModulePath("customer")  .  '/models/CustomerDao.php';
require_once BaseUtil::getModulePath("systemlog")  .  '/services/SystemLogService.php';
require_once BaseUtil::getModulePath("systemlog")  .  '/constants/LogConstant.php';
require_once BaseUtil::getModulePath("component") . '/Number.php';

class WithDrawService extends BaseService {

    private $accountAgentTotal;
    private $accountAgentTradeLog;    
    private $customerDao;
    
    public function __construct(){
        
        parent::__construct();
        
        $this->accountAgentTradeLog = new AccountAgentTradeLogDao();
        $this->accountAgentTradeLog->setDb($this->db);

        $this->accountAgentTotal = new AccountAgentTotalDao();
        $this->accountAgentTotal->setDb($this->db);
        
        $this->customerDao = new CustomerDao();
        $this->customerDao->setDb($this->db);       

//        $this->agent = new AgentDao();
//        $this->agent->setDb($this->db);
    }

    //get withdrawinfo by id
//    public function getWithDrawInfoById($tradeLogId){
//        $agentInfo = $this->agent->getAgentByTradeLogId($tradeLogId);
//        $accountAgentTradeLog = $this->accountAgentTradeLog->getTradeLogById($tradeLogId);
//        $withDrawOnLineInfo = array($agentInfo,$accountAgentTradeLog);
//        return $withDrawOnLineInfo;
//    }

    public function getWithDrawInfoById($tradeLogId){
        $accountAgentTradeLog = $this->accountAgentTradeLog->getTradeLogById($tradeLogId);
        return $accountAgentTradeLog;
    }
    
    public function getAgentTotalByUserId($userId){
    	$agentTotal = $this->accountAgentTotal->getInfoByUserId($userId);
    	return $agentTotal;
    }
    
    // get agent and agent_total and agent_total_log Info
//    public  function getAgentWithDrawInfoById($tradeLogId){
//        $agentInfo = $this->agent->getAgentByTradeLogId($tradeLogId);
//        $accountAgentTradeLog = $this->accountAgentTradeLog->getTradeLogById($tradeLogId);
//        $agentTotalInfo = $this->accountAgentTotal->getAgentTotalByTradeLogId($tradeLogId);
//        $withDrawOnLineInfo = array($agentInfo,$accountAgentTradeLog,$agentTotalInfo);
//        return $withDrawOnLineInfo;
//    }

    //overpass auditing online then the statu will change 4
    public function overPassCheckById($fromWhere,$loginUser,$tradeLogId){
        $log=new SystemLogService();
        $dateNow = date("Y-m-d H:i");
        $updateInfo = array('status'=>4,'modified' =>$dateNow);
        $this->accountAgentTradeLog->updateLastTradeLog($updateInfo,$tradeLogId);
        if($fromWhere == 1){
            $log->insertSystemLog('OLWDP',$loginUser['uname'], array(), $tradeLogId); //overpasscheck online
        }
        if($fromWhere == 2){
            $log->insertSystemLog('OFWDP',$loginUser['uname'], array(), $tradeLogId);//overpasscheck offline
        }
    }
    
    //sure withdraw
    public function sureWithDraw($loginUser,$tradeLogId,$data=array(),$withdrawType = 1){
        $log=new SystemLogService();
        $accountAgentTotalInfo = $this->accountAgentTotal->getAgentTotalByTradeLogId($tradeLogId);
        $accountAgentTradeLogInfo = $this->accountAgentTradeLog->getTradeLogById($tradeLogId);
        $balanceNow = $accountAgentTotalInfo['balance'] - $accountAgentTradeLogInfo['trade_sum'] ;
        $freezeNow = $accountAgentTotalInfo['freezed_balance'] - $accountAgentTradeLogInfo['trade_sum'];
        $debtBalance = $accountAgentTotalInfo["debt_balance"];
        $status = $accountAgentTradeLogInfo['status'];
        $usageBalance = $accountAgentTotalInfo['usage_balance'];
        if($status == 4 && $usageBalance >= 0 && $balanceNow >= 0 && $freezeNow >= 0 && $debtBalance == 0){
            $updateTradeLogInfo = array();
            $updateTradeLogInfo['account_balance_offset'] = 0 - $accountAgentTradeLogInfo['trade_sum'];
            $updateTradeLogInfo['account_freeze_offset'] = 0;
            $updateTradeLogInfo['balance_prev'] = $accountAgentTotalInfo['balance'];
            $updateTradeLogInfo['freeze_prev'] = $accountAgentTotalInfo['freezed_balance'];
            $updateTradeLogInfo['usage_balance_prev'] = $accountAgentTotalInfo['usage_balance'];
            $updateTradeLogInfo['debt_prev'] = $accountAgentTotalInfo['debt_balance'];
            $updateTradeLogInfo['balance_now'] = $balanceNow;
            $updateTradeLogInfo['freeze_now'] = $freezeNow;
            $updateTradeLogInfo['usage_balance_now'] = $accountAgentTotalInfo['usage_balance'];
            $updateTradeLogInfo['debt_now'] = $accountAgentTotalInfo['debt_balance'];
            $updateTradeLogInfo['trade_operator'] = $loginUser['uname'];
            $updateTradeLogInfo['status'] = 5;
            $updateTradeLogInfo['in_date'] = date("Y-m-d H:i:s");
            $updateTradeLogInfo['trade_num'] = $data['trade_num'];
            $updateTradeLogInfo['in_date'] = $data['in_date'];
            $updateTradeLogInfo['out_id'] = $data['out_id'];
            $updateTradeLogInfo['modified'] = date("Y-m-d H:i:s");
            $updateTradeLogInfo['modify_id'] = $loginUser['uname'];
            $this->accountAgentTradeLog->updateLastTradeLog($updateTradeLogInfo,$tradeLogId);//update account_agent_trade_logs
            if($withdrawType == 1){
                $log->insertSystemLog('OLWDS',$loginUser['uname'], $updateTradeLogInfo, $tradeLogId);
            }else{
                $log->insertSystemLog('OFWDS',$loginUser['uname'], $updateTradeLogInfo, $tradeLogId);
            }
            

            $updateTotalInfo = array();
            $updateTotalInfo['balance'] = $accountAgentTotalInfo['balance'] - $accountAgentTradeLogInfo['trade_sum'];
            $updateTotalInfo['freezed_balance'] = $accountAgentTotalInfo['freezed_balance'] - $accountAgentTradeLogInfo['trade_sum'];
            $updateTotalInfo['withdraw_cnt'] = $accountAgentTotalInfo['withdraw_cnt'] + 1;
            $updateTotalInfo['withdraw_sum'] = $accountAgentTotalInfo['withdraw_sum'] + $accountAgentTradeLogInfo['trade_sum'];
            $updateTotalInfo['withdraw_last_time'] = date("Y-m-d H:i:s");
            $updateTotalInfo['modified'] = date("Y-m-d H:i:s");
            $updateTotalInfo['modify_id'] = $loginUser['uname'];
            $id = $accountAgentTotalInfo['id'];
            $this->accountAgentTotal->updateAgent($updateTotalInfo, $id); //update  account_agent_totals
            if($withdrawType == 1){
                $log->insertSystemLog('OLWDS',$loginUser['uname'], $updateTotalInfo, $id);
            }else{
                $log->insertSystemLog('OFWDS',$loginUser['uname'], $updateTotalInfo, $id);
            }
            
        }
    }
    
    //reject withdrow
    public function rejectById($post,$loginUser,$withdrawType = 1){
        $tradeLogId = $post['id'];
        $description = nl2br(trim($post['desctiption']));
        if(!empty($tradeLogId)){
            $log=new SystemLogService();
            $tradeLog = $this->accountAgentTradeLog->getTradeLogById($tradeLogId);
            $accountAgentTotalInfo = $this->accountAgentTotal->getAgentTotalByTradeLogId($tradeLogId);
            if($tradeLog["status"] == 6){
                $tradeSum = $tradeLog['trade_sum'];
                //update trade log
                $updateTradeLogInfo = array();
                $updateTradeLogInfo['account_balance_offset'] = 0;
                $updateTradeLogInfo['account_freeze_offset'] = 0 - $tradeSum;
                $updateTradeLogInfo['account_usage_balance_offset'] = 0 + $tradeSum;
                $updateTradeLogInfo['balance_prev'] = $accountAgentTotalInfo['balance'];
                $updateTradeLogInfo['freeze_prev'] = $accountAgentTotalInfo['freezed_balance'];
                $updateTradeLogInfo['usage_balance_prev'] = $accountAgentTotalInfo['usage_balance'];
                $updateTradeLogInfo['debt_prev'] = $accountAgentTotalInfo['debt_balance'];
                $updateTradeLogInfo['balance_now'] = $accountAgentTotalInfo['balance'];
                $updateTradeLogInfo['freeze_now'] = $accountAgentTotalInfo['freezed_balance'] - $tradeSum;
                $updateTradeLogInfo['usage_balance_now'] = $accountAgentTotalInfo['usage_balance'] + $tradeSum;
                $updateTradeLogInfo['debt_now'] = $accountAgentTotalInfo['debt_balance'];
                $updateTradeLogInfo['trade_operator'] = $loginUser['uname'];
                $updateTradeLogInfo['status'] = 7;
                $updateTradeLogInfo['description'] = $description;
                $updateTradeLogInfo['modified'] = date("Y-m-d H:i:s");
                $updateTradeLogInfo['modify_id'] = $loginUser['uname'];
                $this->accountAgentTradeLog->updateLastTradeLog($updateTradeLogInfo,$tradeLogId);//update account_agent_trade_logs
                if($withdrawType == 1){
                    $log->insertSystemLog('OLWDR',$loginUser['uname'], $updateTradeLogInfo, $tradeLogId);
                }else{
                    $log->insertSystemLog('OFWDR',$loginUser['uname'], $updateTradeLogInfo, $tradeLogId);
                }
                //update trade total
                $updateTotalInfo = array();
                $updateTotalInfo['freezed_balance'] = $accountAgentTotalInfo['freezed_balance'] - $tradeSum;
                $updateTotalInfo['usage_balance'] = $accountAgentTotalInfo['usage_balance'] + $tradeSum;
                $updateTotalInfo['modified'] = date("Y-m-d H:i:s");
                $updateTotalInfo['modify_id'] = $loginUser['uname'];
                $id = $accountAgentTotalInfo['id'];
                $this->accountAgentTotal->updateAgent($updateTotalInfo, $id); //update  account_agent_totals
                if($withdrawType == 1){
                    $log->insertSystemLog('OLWDR',$loginUser['uname'], $updateTotalInfo, $id);
                }else{
                    $log->insertSystemLog('OFWDR',$loginUser['uname'], $updateTotalInfo, $id);
                }
            }
        }
    }

    //add withdraw application
    public function offLineApplication($loginUser,$data){
        $log=new SystemLogService();
        $userId = $this->getUserByName($data['account']);
        if(!empty($userId)){
            $accountAgentTotalInfo = $this->accountAgentTotal->getInfoByUserId($userId);
            $usage_balance = $accountAgentTotalInfo['usage_balance'] - $data['trade_sum'];
	        if($usage_balance >= 0){
	            $insertInfo['user_id'] = $userId;
	            $insertInfo['trade_type'] = 3;
	            $insertInfo['trade_sum'] = $data['trade_sum'];
	            $insertInfo['account_balance_offset'] = 0;
	            $insertInfo['account_freeze_offset'] = 0 + $data['trade_sum'];
	            $insertInfo['account_usage_balance_offset'] = 0 - $data['trade_sum'];
	            $insertInfo['balance_prev'] = $accountAgentTotalInfo['balance'];
	            $insertInfo['freeze_prev'] = $accountAgentTotalInfo['freezed_balance'];
	            $insertInfo['usage_balance_prev'] = $accountAgentTotalInfo['usage_balance'];
	            $insertInfo['debt_prev'] = $accountAgentTotalInfo['debt_balance'];
	            $insertInfo['balance_now'] = $accountAgentTotalInfo['balance'];
	            $insertInfo['freeze_now'] = $accountAgentTotalInfo['freezed_balance'] + $data['trade_sum'];
	            $insertInfo['usage_balance_now'] = $accountAgentTotalInfo['usage_balance'] - $data['trade_sum'];
	            $insertInfo['debt_now'] = $accountAgentTotalInfo['debt_balance'];
	            $insertInfo['withdraw_type'] = $data['in_bank'];
	            $insertInfo['trade_operator'] =  $loginUser['uname'];
	            $insertInfo['in_id'] = $data['in_id'];
	            $insertInfo['in_name'] = $data['in_name'];
	            $insertInfo['in_bank_name'] = $data['in_bank_name'];
	            $insertInfo['status'] = 6;
	            $insertInfo['modified'] = date("Y-m-d H:i");
	            $insertInfo['modify_id'] = $loginUser['uname'];
	            $insertInfo['created'] = date("Y-m-d H:i");
	            $insertInfo['create_id'] = $loginUser['uname'];
	            $tradeLogId = $this->accountAgentTradeLog -> insertTradeLog($insertInfo); //after insert the sn is empty
	            $set = array();
	            $set['sn'] = Number::getTradeLogSN($tradeLogId, 3); // get the sn number
	            $this->accountAgentTradeLog->updateLastTradeLog($set, $tradeLogId); //update the sn
	            $insertInfo['sn'] = $set['sn'];
	            $log->insertSystemLog('AOFA',$loginUser['uname'], $insertInfo);
	
	            $updateTotalInfo = array();
	            $updateTotalInfo['usage_balance'] = $accountAgentTotalInfo['usage_balance'] - $data['trade_sum'];
	            $updateTotalInfo['freezed_balance'] = $accountAgentTotalInfo['freezed_balance'] + $data['trade_sum'];
	            $updateTotalInfo['modified'] = date("Y-m-d H:i");
	            $updateTotalInfo['modify_id'] = $loginUser['uname'];
	            $agentTotalId = $accountAgentTotalInfo['id'];
	            $this->accountAgentTotal->updateAgent($updateTotalInfo, $agentTotalId); //update account_agent_totals
	            $log->insertSystemLog('AOFA',$loginUser['uname'], $updateTotalInfo, $agentTotalId); //add system log
	        }
        }
    }
    
    private function getUserByName($name){
    	 $user = $this->customerDao->getCustomerByName($name);
    	 return $user['id'];
    }

    //sure withdraw offline
//    public function surewithdrawoffline($loginUser,$data , $tradeLogId ,$adminId){
//        $log=new SystemLogService();
//        $accountAgentTotalInfo = $this->accountAgentTotal->getAgentTotalByTradeLogId($tradeLogId);
//        $accountAgentTradeLogInfo = $this->accountAgentTradeLog->getTradeLogById($tradeLogId);
//        $balance_now = $accountAgentTotalInfo['balance'] - $accountAgentTradeLogInfo['trade_sum'] ;
//        $freeze_now = $accountAgentTotalInfo['freezed_balance'] - $accountAgentTradeLogInfo['trade_sum'];
//        if($balance_now >= 0 && $freeze_now >= 0){
//            $updateTradeLogInfo = array();
//            $updateTradeLogInfo['trade_num'] = $data['trade_num'];
//            $updateTradeLogInfo['account_balance_offset'] = 0 - $accountAgentTradeLogInfo['trade_sum'];
//            $updateTradeLogInfo['account_freeze_offset'] = 0;
//            $updateTradeLogInfo['balance_prev'] = $accountAgentTotalInfo['balance'];
//            $updateTradeLogInfo['freeze_prev'] = $accountAgentTotalInfo['freezed_balance'];
//            $updateTradeLogInfo['usage_balance_prev'] = $accountAgentTotalInfo['usage_balance'];
//            $updateTradeLogInfo['balance_now'] = $balance_now;
//            $updateTradeLogInfo['freeze_now'] = $freeze_now;
//            $updateTradeLogInfo['usage_balance_now'] = $accountAgentTotalInfo['usage_balance'];
//            $updateTradeLogInfo['trade_operator'] = $adminId;
//            $updateTradeLogInfo['out_id'] = $data['out_id'];
//            $updateTradeLogInfo['out_date'] = $data['out_date'];
//            $updateTradeLogInfo['status'] = 5;
//            $updateTradeLogInfo['update_time'] = date("Y-m-d H:i");
//            $updateTradeLogInfo['update_id'] = $adminId;
//            $this->accountAgentTradeLog->updateLastTradeLog($updateTradeLogInfo,$tradeLogId);//update account_agent_trade_logs
//            $log->insertSystemLog('UAT',$loginUser['uname'], array(), $tradeLogId);
//
//            $updateTotalInfo['balance'] = $accountAgentTotalInfo['balance'] - $accountAgentTradeLogInfo['trade_sum'];
//            $updateTotalInfo['freezed_balance'] = $accountAgentTotalInfo['freezed_balance'] - $accountAgentTradeLogInfo['trade_sum'];
//            $updateTotalInfo['withdraw_cnt'] = $accountAgentTotalInfo['withdraw_cnt'] + 1;
//            $updateTotalInfo['withdraw_sum'] = $accountAgentTotalInfo['withdraw_sum'] + $accountAgentTradeLogInfo['trade_sum'];
//            $updateTotalInfo['withdraw_last_time'] = date("Y-m-d H:i");
//            $updateTotalInfo['update_time'] = date("Y-m-d H:i");
//            $updateTotalInfo['update_id'] = $adminId;
//            $id = $accountAgentTotalInfo['id'];
//            $this->accountAgentTotal->updateAgent($updateTotalInfo, $id); //update  account_agent_totals
//            $log->insertSystemLog('OFLWDS',$loginUser['uname'], array(), $id);
//        }
//    }

    //delete tradelog
//    public function deleteTradeLogById($fromWhere,$tradeLogId){
//        $this->accountAgentTradeLog->deleteTradeLog($tradeLogId);
//        if($fromWhere == "online"){
//            $log->insertSystemLog('OLWDD',$loginUser['uname'], array(), $tradeLogId);
//        }
//        if($fromWhere == "offline"){
//            $log->insertSystemLog('OFLWDD',$loginUser['uname'], array(), $tradeLogId);
//        }
//    }

//       public function deleteTradeLog($tradeLogId,$loginUser,$action = "OLWDD"){
//            $log=new SystemLogService();
//            $tradeLog = $this->accountAgentTradeLog->getTradeLogById($tradeLogId);
//            $agentTotalInfo = $this->accountAgentTotal->getAgentTotalByTradeLogId($tradeLogId);
//            $this->accountAgentTradeLog->deleteTradeLog($tradeLogId);
//            //update agent total put freeze to useage 
//            if($tradeLog["status"] == 4 || $tradeLog["status"] == 6){
//                $tradeSum = $tradeLog['trade_sum'];
//                $updateTotalInfo = array();
//                $updateTotalInfo['freezed_balance'] = $agentTotalInfo['freezed_balance'] - $tradeSum;
//                $updateTotalInfo['usage_balance'] = $agentTotalInfo['usage_balance'] + $tradeSum;
//                $updateTotalInfo['update_time'] = date("Y-m-d H:i:s");
//                $updateTotalInfo['update_id'] = $loginUser['uname'];
//                $this->accountAgentTotal->updateAgent($updateTotalInfo, $agentTotalInfo['id']);
//            }
//            $log->insertSystemLog($action,$loginUser['uname'], array(), $tradeLogId);
//       }
}


?>
